// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Belt Platform Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 57 : TYPE_BELTPLATFORM

// Function declarations
reserve function BeltPlatform_Function131

// Static Values

// Tables
public table BeltPlatform_table41
	0, 1, 2, 3, 4, 3, 2, 1
end table

public table BeltPlatform_table40
	0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 1, 1, 1
end table

public table BeltPlatform_table39
	6, 6, 6, 6, 6, 6
end table

public table BeltPlatform_table38
	0, 0, 0, 0, 0, 0
end table

private table BeltPlatform_table0
	0xE14, 880, 0xEF0, 770, 0xEF0, 832, 0xE14, 942
end table

private table BeltPlatform_table1
	0xF14, 736, 0xFF0, 626, 0xFF0, 688, 0xF14, 798
end table

private table BeltPlatform_table2
	0x1014, 624, 0x10F0, 514, 0x10F0, 576, 0x1014, 686
end table

private table BeltPlatform_table3
	0xF14, 0x570, 0xFF0, 0x502, 0xFF0, 0x540, 0xF14, 0x5AE
end table

private table BeltPlatform_table4
	0x1B14, 0x670, 0x1BF0, 0x602, 0x1BF0, 0x640, 0x1B14, 0x6AE
end table

private table BeltPlatform_table5
	0x1C14, 0x5E0, 0x1CF0, 0x572, 0x1CF0, 0x5B0, 0x1C14, 0x61E
end table

function BeltPlatform_Function131
	temp0 = object[arrayPos0].value5
	GetTableValue(object[arrayPos0].value0, temp0, object[arrayPos0].value4)
	temp0++
	GetTableValue(object[arrayPos0].value1, temp0, object[arrayPos0].value4)
	temp0 = object[arrayPos0].ixpos
	temp0 -= object[arrayPos0].value0
	Abs(temp0)
	temp1 = object[arrayPos0].iypos
	temp1 -= object[arrayPos0].value1
	Abs(temp1)
	if temp1 < temp0
		object[arrayPos0].xvel = 0x10000
		temp1 <<= 16
		object[arrayPos0].yvel = temp1
		object[arrayPos0].yvel /= temp0
	else
		if temp1 != 0
			temp0 <<= 16
			object[arrayPos0].xvel = temp0
			object[arrayPos0].xvel /= temp1
		else
			object[arrayPos0].xvel = 0
		end if
		object[arrayPos0].yvel = 0x10000
	end if
	if object[arrayPos0].value0 < object[arrayPos0].ixpos
		FlipSign(object[arrayPos0].xvel)
	end if
	if object[arrayPos0].value1 < object[arrayPos0].iypos
		FlipSign(object[arrayPos0].yvel)
	end if
end function


event ObjectMain
	temp0 = object.xpos
	temp1 = object.ypos
	temp0 >>= 16
	temp1 >>= 16
	if temp0 == object.value0
		if temp1 == object.value1
			object.value5 += 2
			if object.value5 > object.value6
				object.value5 = 0
			end if
			arrayPos0 = object.entityPos
			CallFunction(BeltPlatform_Function131)
		end if
	end if
	if object.rotation != 0
		object.rotation++
		object.rotation &= 15
	else
		if object.value5 > 2
			object.rotation++
		end if
	end if
	temp0 = object.rotation
	GetTableValue(object.direction, temp0, BeltPlatform_table40)
	temp0 &= 7
	GetTableValue(object.frame, temp0, BeltPlatform_table41)
	temp0 = object.xpos
	temp0 &= -0x10000
	temp1 = object.ypos
	temp1 &= -0x10000
	temp2 = object.xpos
	temp2 += object.xvel
	temp3 = object.ypos
	temp3 += object.yvel
	object.value2 = temp2
	object.value2 &= -0x10000
	object.value2 -= temp0
	object.value3 = temp3
	object.value3 &= -0x10000
	object.value3 -= temp1
	if object.rotation == 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
			temp7 = checkResult
			CheckLower(object[0].value1, 20)
			temp7 &= checkResult
			if temp7 == 0			
				BoxCollisionTest(C_BOX, object.entityPos, -16, -7, 16, 7, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 1
					object[currentPlayer].xpos += object.value2
					object[currentPlayer].ypos += object.value3
				end if
			end if	
			if object[currentPlayer].state == PlayerObject_HandleMonikaTeleport
				if object[0].value1 >= 14
					BoxCollisionTest(C_TOUCH, object.entityPos, -16, -7, 16, 7, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					if checkResult == 1
						if object[currentPlayer].ypos > object.ypos
							object[currentPlayer].iypos = object[currentPlayer].collisionBottom
							FlipSign(object[currentPlayer].ypos)
							object[currentPlayer].ypos += 0x140000
							object[currentPlayer].ypos += object.ypos
							if object[currentPlayer].yvel < 0
								object[currentPlayer].yvel = 0
							end if
						else
							object[currentPlayer].iypos = object[currentPlayer].collisionTop
							FlipSign(object[currentPlayer].ypos)
							object[currentPlayer].ypos -= 0x140000
							object[currentPlayer].ypos += object.ypos
							if object[currentPlayer].yvel > 0
								object[currentPlayer].yvel = 0
							end if
						end if		
					end if	
				end if	
			end if				
		next
	end if
	object.xpos = temp2
	object.ypos = temp3
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-16, -7, 32, 14, 413, 166)
	SpriteFrame(-16, -12, 32, 25, 446, 169)
	SpriteFrame(-15, -15, 30, 30, 480, 166)
	SpriteFrame(-13, -16, 25, 32, 449, 198)
	SpriteFrame(-7, -16, 14, 32, 480, 198)
	SetTableValue(BeltPlatform_table0, 0, BeltPlatform_table38)
	SetTableValue(BeltPlatform_table1, 1, BeltPlatform_table38)
	SetTableValue(BeltPlatform_table2, 2, BeltPlatform_table38)
	SetTableValue(BeltPlatform_table3, 3, BeltPlatform_table38)
	SetTableValue(BeltPlatform_table4, 4, BeltPlatform_table38)
	SetTableValue(BeltPlatform_table5, 5, BeltPlatform_table38)
	foreach (TypeName[Belt Platform], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_INACTIVE
		temp0 = object[arrayPos0].propertyValue
		temp0 >>= 4
		GetTableValue(object[arrayPos0].value4, temp0, BeltPlatform_table38)
		GetTableValue(object[arrayPos0].value6, temp0, BeltPlatform_table39)
		object[arrayPos0].value5 = object[arrayPos0].propertyValue
		object[arrayPos0].value5 &= 15
		object[arrayPos0].value5 <<= 1
		CallFunction(BeltPlatform_Function131)
	next
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
